package com.czy.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * @Author: czy
 * @Date: 2019/5/22 15:44
 */
public final class DateUtil {
    private DateUtil() {}

    public static final String PATTERN_DATE_SHORT = "yyyyMMdd";
    public static final String PATTERN_DATE_MS = "yyyyMMddHHmmss";
    public static final String PATTERN_DATE_HH = "yyyyMMddHH";
    public static final String PATTERN_DATE = "yyyy-MM-dd";

    public static final String PATTERN_MONTH = "yyyy-MM";

    public static final String PATTERN_DATE_TIME_MS = "yyyy-MM-dd HH:mm:ss";

    public static final String PATTERN_DATE_TIME = "yyyy-MM-dd HH:mm:ss";

    public static final String PATTERN_DATE_MINUTES = "yyyy-MM-dd HH:mm";

    public static final String PATTERN_DATE_HOURS = "yyyy-MM-dd HH";

    public static final String PATTERN_DATE_TIME_SHORT = "yyyy-MM-dd-HH-mm-ss";

    /**
     * 获取当前时间日期(未格式化)
     * @return  Wed May 22 15:52:10 CST 2019
     */
    public static Date getDate(){
        Calendar instance = Calendar.getInstance();
        return instance.getTime();
    }

    /**
     * 获取当前时间日期(格式化后的)
     * @return  2019-05-22 16:01:34
     */
    public static String getCurrentDateTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN_DATE_TIME_MS);
        String formatDate = simpleDateFormat.format(getDate());
        return formatDate;
    }

    /**
     * 获取当前时间日期(格式化后的)
     * @return  2019-05-22
     */
    public static String getCurrentDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN_DATE);
        String time = simpleDateFormat.format(getDate());
        return time;
    }

    /**
     * 解析字符串形式的日期
     * @param time
     * @return  Wed May 22 16:01:34 CST 2019
     */
    public static Date parseDate(String time){
        Date parseDate=null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN_DATE_TIME_MS);
            parseDate = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parseDate;
    }

    /**
     * 获取当前时间戳
     * @return  1558511684484
     */
    public static String getCurrentDateStr(){
        return String.valueOf(getDate().getTime());
    }

    /**
     * 获取年
     * @return
     */
    public static int getYear(){
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 获取月
     * @return
     */
    public static int getMonth(){
        return Calendar.getInstance().get(Calendar.MONTH)+1;
    }

    /**
     * 获取月中的天
     * @return
     */
    public static int getDayOfMonth(){
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取星期
     * @return
     */
    public static String getDayOfWeek(){
        String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        int i = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1;
        return weeks[i];
    }

    /**
     * 月中的第几个星期
     * @return
     */
    public static int getWeekOfMonth(){
        return Calendar.getInstance().get(Calendar.WEEK_OF_MONTH)-1;
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }
}
